package com.crm.system.controller.userForumManager;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.userForumManager.UserForum;
import com.crm.model.qo.userForumManager.UserForumQO;
import com.crm.model.vo.customerManager.AnalyseCustomerRedisVO;
import com.crm.model.vo.userForumManager.GodTopicVO;
import com.crm.model.vo.userForumManager.MostSearchVO;
import com.crm.model.vo.userForumManager.UserForumInfoVO;
import com.crm.model.vo.userForumManager.UserForumVO;
import com.crm.redis.service.userForumManager.UserForumRedisService;
import com.crm.system.service.userForumManager.UserForumService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:chaifuyou
 * @Date: 2021/5/24 9:29
 */
@RestController
@RequestMapping("/userForum")
@Slf4j
public class UserForumController {

    @Autowired
    private UserForumService userForumService;

    @Autowired
    private UserForumRedisService userForumRedisService;

    /**
     * 分页查询论坛
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param userForumQO
     * @return
     */
    @GetMapping("/userForumListPage")
    public DataResponse selectUserForumListPage(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                                @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                                UserForumQO userForumQO//查询条件
    ){
        DhPageInfo<UserForumVO> userForumListPage = userForumService.selectUserForumListPage(pageIndex, pageSize, sortName, sortOrder, userForumQO);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), userForumListPage);
    }

    /**
     * 发布论坛
     * @param userForum
     * @return
     */
    @PostMapping("/addUserForum")
    public DataResponse addUserForum(HttpServletRequest request, @RequestBody UserForum userForum){
        UserForumVO userForumVO=userForumService.addUserForum(userForum);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),userForumVO);
    }

    /**
     * 删除论坛
     * @param userForumId
     * @return
     */
    @PostMapping("/delUserForum")
    public DataResponse delUserForum(HttpServletRequest request, @RequestParam String userForumId){
        boolean b=userForumService.delUserForum(userForumId);
        if (!b){
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 返回优秀话题
     * @param
     * @return
     */
    @PostMapping("/selectGodTopic")
    public DataResponse selectGodTopic(){
        String jsonDate = userForumRedisService.selectGodTopic();
        JSONArray jsonObject = JSONObject.parseArray(jsonDate);
        List<GodTopicVO> list = JSONArray.toJavaObject(jsonObject, List.class);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }

    /**
     * 返回热力搜索
     * @param
     * @return
     */
    @PostMapping("/selectMostSearch")
    public DataResponse selectMostSearch(){
        List<MostSearchVO> mostSearchList = new ArrayList<>();

        MostSearchVO mostSearch = new MostSearchVO();
        mostSearch.setSearchName("怎么可以这么热");
        for (int i=1;i<9;i++){
            mostSearchList.add(mostSearch);
        }
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),mostSearchList);
    }

    /**
     * 个人论坛信息
     * @param
     * @return
     */
    @PostMapping("/selectLoginUserForumInfo")
    public DataResponse selectLoginUserForumInfo(@RequestParam(required = false) String accountId){
        UserForumInfoVO vo = userForumService.selectLoginUserForumInfo(accountId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),vo);
    }
}
